package com.house.web.controller.system;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.house.common.core.controller.BaseController;
import com.house.common.core.domain.AjaxResult;
import com.house.common.core.domain.entity.House;
import com.house.common.core.page.TableDataInfo;
import com.house.system.service.IHouseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.house.system.domain.HouseSupport;
import com.house.system.service.IHouseSupportService;

/**
 * 房屋家具类Controller
 *
 * @author tc
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/domain/support")
public class HouseSupportController extends BaseController {

    @Autowired
    private IHouseSupportService houseSupportService;

    @Autowired
    private IHouseService houseService;

    /**
     * 查询房屋家具类列表
     */
    @PreAuthorize("@ss.hasPermi('domain:support:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseSupport houseSupport) {
        startPage();
        List<HouseSupport> list = houseSupportService.selectHouseSupportList(houseSupport);
        return getDataTable(list);
    }

    /**
     * 查询房屋已分配家具列表
     */
    @PreAuthorize("@ss.hasPermi('domain:support:list')")
    @GetMapping("/allotSupport/allocatedList/{houseId}")
    public AjaxResult allocatedList(@PathVariable("houseId") Long houseId) {

        AjaxResult ajax = AjaxResult.success();
        House house = houseService.selectHouseByHouseId(houseId);
        List<HouseSupport> supports = houseSupportService.selectAllocatedList(houseId);
        ajax.put("house", house);
        ajax.put("supports", supports);
        return ajax;
    }


    /**
     * 获取房屋家具类详细信息
     */
    @PreAuthorize("@ss.hasPermi('domain:support:query')")
    @GetMapping(value = "/{houseId}")
    public AjaxResult getInfo(@PathVariable("houseId") Long houseId) {
        return AjaxResult.success(houseSupportService.selectHouseSupportByHouseId(houseId));
    }

    /**
     * 新增房屋家具类
     */
    @PreAuthorize("@ss.hasPermi('domain:support:add')")
    @PostMapping
    public AjaxResult add(@RequestBody HouseSupport houseSupport) {
        return toAjax(houseSupportService.insertHouseSupport(houseSupport));
    }

    /**
     * 修改房屋家具类
     */
    @PreAuthorize("@ss.hasPermi('domain:support:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody HouseSupport houseSupport) {
        return toAjax(houseSupportService.updateHouseSupport(houseSupport));
    }

    /**
     * 删除房屋家具类
     */
    @PreAuthorize("@ss.hasPermi('domain:support:remove')")
    @DeleteMapping("/{houseIds}")
    public AjaxResult remove(@PathVariable Long[] houseIds) {
        return toAjax(houseSupportService.deleteHouseSupportByHouseIds(houseIds));
    }

    /**
     * 修改房屋配套家具
     */
    @PreAuthorize("@ss.hasPermi('domain:support:edit')")
    @PutMapping("/allotSupport")
    public AjaxResult insertSupport(Long houseId, String[] supports) {
        houseSupportService.insertAllotSupport(houseId,supports);
        return success();
    }
}
